//
//  BDRemindTool.m
//  ProjectforHDX
//
//  Created by Jaesun on 16/11/22.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "BDRemindTool.h"

@implementation BDRemindTool

/**
 判断是否为第一次进入APP

 @return 是 YES
 */
+ (void)setRemindTypeAtFirstStartApp {
    // BOOL类型默认为NO,第一次进入APP设为YES,下次进入这个是就是YES了.
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"set_remind_at_first"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"set_remind_at_first"];
        [self setRemindCanRing:YES];
        [self setRemindCanVibrate:YES];
    }
}

+ (void)setRemindCanRing:(BOOL)canRing canVibrate:(BOOL)canVibrate {
    
    if (canRing && canVibrate) {
        [[NSUserDefaults standardUserDefaults] setInteger:3 forKey:REMIND_TYPE];
    }
    else if (canRing) {
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:REMIND_TYPE];
    }
    else if (canVibrate) {
        [[NSUserDefaults standardUserDefaults] setInteger:2 forKey:REMIND_TYPE];
    }
    else {
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:REMIND_TYPE];
    }
}

+ (void)setRemindCanRing:(BOOL)canRing {
    
    [[NSUserDefaults standardUserDefaults] setBool:canRing forKey:REMIND_RING];
    BOOL canVibrate = [[NSUserDefaults standardUserDefaults] boolForKey:REMIND_VIBRATE];
    [self setRemindCanRing:canRing canVibrate:canVibrate];
}

+ (BOOL)getRemindCanRing {
    BOOL canRing = [[NSUserDefaults standardUserDefaults] boolForKey:REMIND_RING];
    return canRing;
}

+ (void)setRemindCanVibrate:(BOOL)canVibrate {
    [[NSUserDefaults standardUserDefaults] setBool:canVibrate forKey:REMIND_VIBRATE];
    BOOL canRing = [[NSUserDefaults standardUserDefaults] boolForKey:REMIND_RING];
    [self setRemindCanRing:canRing canVibrate:canVibrate];
}

+ (BOOL)getRemindVibrate {
    
    BOOL canVibrate = [[NSUserDefaults standardUserDefaults] boolForKey:REMIND_VIBRATE];
    return canVibrate;
}

/**
 获取提醒方式 静音 0/铃声 1/振动 2/ 铃声+振动 3
 
 @param canRing    是否可以铃声
 @param canVibrate 是否可以振动
 */
+ (NSInteger)getRemindType {
    NSInteger remindType = [[NSUserDefaults standardUserDefaults] integerForKey:REMIND_TYPE];
    return remindType;
}

/**
 只振动
 */
+ (void)vibrateOnly {
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

/**
 只响铃
 */
+ (void)palySoundOnly
{
//    SystemSoundID soundId;
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"caf"];
//    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundId);
    AudioServicesPlaySystemSound(1007);
}

/**
 铃声➕振动
 */
+ (void)palySoundAndVibrate {
    
//    SystemSoundID sound;
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"caf"];
//    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &sound);
    // 播放一次后回调,在回调中重复振动
   // AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, NULL, NULL, soundCompleteCallback, NULL);
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    AudioServicesPlaySystemSound(1007);
}


/**
 连续振动回调

 @param sound      声音ID
 @param clientData
 */
void soundCompleteCallback(SystemSoundID sound,void * clientData) {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  //震动
        AudioServicesPlaySystemSound(sound);
    });
    //AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
}

#pragma mark- 设置提醒方式 静音/铃声/振动/铃声+振动
+ (void)remind {

    NSInteger remindType = [self getRemindType];
    switch (remindType) {
        case 0:
            return;
            break;
        case 1: {
            [self palySoundOnly];
        }
            break;
        case 2:{
            [self vibrateOnly];
        }
            break;
        case 3:{
            [self palySoundAndVibrate];
        }
            break;
        default:
            break;
    }
}

/**
 停止响铃和震动

 @param soundID 铃声ID
 */
+ (void)stopSound:(SystemSoundID)soundID {
   
    if (soundID) {
        AudioServicesRemoveSystemSoundCompletion(soundID);
    }
    AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
}


@end
